<template>
  <div class="manage-member">
    <div class="member-header">
      <el-button type="primary" size="small" @click="dialogFormVisible = true">添加成员</el-button>
      <el-button type="danger" size="small" @click="delMany()">批量删除</el-button>
    </div>
    <el-table :data="memberlist" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"> </el-table-column>

      <el-table-column prop="user_name" label="姓名"> </el-table-column>
      <el-table-column prop="oa_user_name" label="用户名"> </el-table-column>

      <el-table-column prop="department_name" label="部门"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-popconfirm
            confirm-button-text="好的"
            cancel-button-text="不用了"
            icon="el-icon-info"
            icon-color="red"
            title="确定删除吗？"
            @onConfirm="handleDelete(scope.row)"
          >
            <el-button
              slot="reference"
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
            ></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="float: right; margin-top: 8px">
      <el-pagination
        :current-page="pageParams.page"
        :page-size="10"
        :total="totalCount"
        layout="total, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
    <el-dialog :visible.sync="dialogFormVisible" title="添加成员" width="400px">
      <el-form ref="form" :model="form" :rules="rules" label-width="90px" label-position="right">
        <el-form-item label="成员:" prop="users">
          <el-select v-model="form.users" multiple placeholder="请选择" filterable>
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.department_name + '-' + item.user_name"
              :value="item.oa_user_name"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addMember">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "ManageMember",
  data() {
    return {
      userList: [],
      multipleSelection: [],
      pageParams: {
        page: 1
      },
      totalCount: 0,
      memberlist: [],
      dialogFormVisible: false,
      form: {
        users: []
      },
      rules: {
        users: [{ required: true, message: "请选择成员", trigger: "change" }]
      }
    };
  },
  watch: {
    dialogFormVisible(val) {
      if (!val) {
        this.form.id = "";
        this.$refs.form.resetFields();
      }
    }
  },
  created() {
    this.initData();
    this.$http.member.getAlluser().then(res => {
      this.userList = res;
    });
  },
  methods: {
    delMany() {
      if (this.multipleSelection.length == 0) return;
      this.$confirm(`此操作将永久删除${this.multipleSelection.length}条, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http.member
            .delMember({
              $query: {
                id: this.$route.query.project_id
              },
              $body: this.multipleSelection
            })
            .then(res => {
              this.$message({
                message: "删除成功",
                type: "success"
              });
              this.initData();
            });
        })
        .catch(() => {});
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map(item => item.oa_user_name);
    },
    handleCurrentChange(val) {
      this.pageParams.page = val;
      this.initData();
    },
    initData() {
      this.$http.member
        .getMemberList({ id: this.$route.query.project_id, ...this.pageParams })
        .then(res => {
          this.totalCount = res.total;
          this.memberlist = res.items;
        });
    },
    handleDelete(row) {
      this.$http.member
        .delMember({
          $query: {
            id: this.$route.query.project_id
          },
          $body: [row.oa_user_name]
        })
        .then(res => {
          this.$message({
            message: "删除成功",
            type: "success"
          });
          this.initData();
        });
    },
    addMember() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.$http.member
            .addMembers({
              $query: {
                id: this.$route.query.project_id
              },
              $body: Array.from(this.form.users)
            })
            .then(res => {
              this.$message({
                message: "添加成功",
                type: "success"
              });
              this.dialogFormVisible = false;
              this.initData();
            });
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.manage-member {
  .member-header {
    float: right;
    margin-bottom: 8px;
  }
}
/deep/.el-table__header-wrapper .el-table__header th {
  background-color: #f8f8f9;
  color: #515a6e;
  font-size: 14px;
  font-weight: 500;
}
.tips {
  font-size: 14px;
  color: #dd5044;
  text-align: center;
}
</style>
